package com.yeyaomai.dksns.flowis.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import com.yeyaomai.dksns.flowis.dao.FlowRoleDao;
import com.yeyaomai.dksns.flowis.dao.FlowUserDao;
import com.yeyaomai.dksns.flowis.domain.FlowRole;
import com.yeyaomai.dksns.flowis.domain.FlowUser;
import com.yeyaomai.dksns.flowis.page.PageImplResult;
import com.yeyaomai.dksns.flowis.page.PageRequestOrder;
import com.yeyaomai.dksns.flowis.page.SplitConstant;
import com.yeyaomai.dksns.flowis.service.FlowUserService;

@Service
public class FlowUserServiceImpl implements FlowUserService {

	@Resource
	private FlowUserDao flowUserDao;

	@Resource
	private FlowRoleDao flowRoleDao;

	@Override
	public void addFlowUser(FlowUser flowUser) {
		String userName = flowUser.getUserName();
		flowUserDao.addFlowUser(flowUser);
		FlowUser user = flowUserDao.getFlowUserByUserName(userName);
		FlowRole flowRole = new FlowRole();
		flowRole.setUserId(user.getUserId());
		flowRole.setRoleId(flowUser.getRoleId());
		flowRoleDao.addFlowRole_User(flowRole);
	}

	@Override
	public boolean isExistFlowUser(String userName) {
		FlowUser flowUser = new FlowUser();
		flowUser = flowUserDao.getFlowUserByUserName(userName);
		if (null == flowUser) {
			return true;
		} else {
			return false;
		}

	}

	public Page<FlowUser> listFlowUser(Integer currentpage) {
		Integer flowUserCount = flowUserDao.countFlowUser();
		PageRequestOrder request = new PageRequestOrder(currentpage,
				SplitConstant.PAGE_SIZE);
		List<FlowUser> flowUserList = new ArrayList<FlowUser>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startNumber", request.start());
		map.put("endNumber", request.end());
		flowUserList = flowUserDao.listFlowUser(map);
		FlowRole flowRole = new FlowRole();
		for (int i = 0; i < flowUserList.size(); i++) {
			flowRole = flowRoleDao.getFlowRoleUserByUserId(flowUserList.get(i)
					.getUserId());
			if(null != flowRole)
			{
				flowUserList.get(i).setRoleName(flowRole.getRoleName());
			}
			else
			{
				flowUserList.get(i).setRoleName("无");
			}
			
		}
		Page<FlowUser> result = new PageImplResult<FlowUser>(flowUserList,
				request, flowUserCount);
		return result;
	}

	public void deleteFlowUser(Long userId) {
		flowRoleDao.deleteRole_User(userId);
		flowUserDao.deleteFlowUser(userId);
	}

	public void updateFlowUser(FlowUser flowUser) {
		Long userId = flowUser.getUserId();
		flowUserDao.updateFlowUser(flowUser);
		Long roleId = flowUser.getRoleId();
		if(null != roleId)
		{
			flowRoleDao.deleteRole_User(userId);
			FlowRole flowRole = new FlowRole();
			flowRole.setUserId(userId);
			flowRole.setRoleId(roleId);
			flowRoleDao.addFlowRole_User(flowRole);
		}
		
	}

	public FlowUser getFlowUserByUserId(Long userId) {
		return flowUserDao.getFlowUserByUserId(userId);
	}
	
	@Override
	public FlowUser getFlowUserByUserName(String userName) {
		return flowUserDao.getFlowUserByUserName(userName);
	}
	
	@Override
	public FlowUser loginCheck(FlowUser user) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userName", user.getUserName());
		map.put("password", user.getPassword());
		return flowUserDao.loginCheck(map);
	}
}
